package com.library.lockProvider.service.impl;

import com.library.lockProvider.mapper.BoxAndCabinetMapper;
import com.library.lockProvider.mapper.CabinetMapper;
import com.library.lockProvider.pojo.boxAndCabinet.BoxAndCabinet;
import com.library.lockProvider.pojo.Cabinet.CabinetResultDto;
import com.library.lockProvider.pojo.Cabinet.Cabinet;
import com.library.lockProvider.pojo.Cabinet.CabinetResult;
import com.library.lockProvider.service.CabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("cabinetService")
public class CabinetServiceImpl implements CabinetService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CabinetMapper cabinetMapper;

    @Autowired
    private BoxAndCabinetMapper boxAndCabinetMapper;

    private static String url="https://open.qianmingyun.com/webapi/apiService/v1/s1";
    //private static String cabinetId="321562982";
   // private static final String APPKEY="GdhYWtLfhEZV2vx3DxnmV5wYVgFjz9Xm";
    //private static final String SCRETEKEY="r6HXwwwffHGqgvttnZHszYOLUOPT7BuL";


    @Override
    @Transactional
    public String checkIsOnline(int cabinetId) throws UnsupportedEncodingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&timeStamp="+longStamp+cabinet.getSecretKey();
        String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cabinetId",cabinet.getCabinetId());
        map.put("timeStamp",longStamp);
        map.put("sign",sign);
        map.put("appKey",cabinet.getAppKey());
        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
        String body = restTemplate.postForObject(url+"/chkDevice",request,String.class);
        return body;
    }

    @Override
    @Transactional
    public CabinetResult set_deviceName(int cabinetId, String cabinetName) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&cabinetName="+cabinetName+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("cabinetName",cabinetName);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            restTemplate.postForObject(url+"/set_deviceName",request, CabinetResultDto.class);
        }

        return ans;
    }

    @Override
    @Transactional
    public CabinetResult set_qrParam(int cabinetId, String qrCode) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&qrCode="+qrCode+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("qrCode",qrCode);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crd=restTemplate.postForObject(url+"/set_qrParam",request, CabinetResultDto.class);
            if(crd!=null){
                ans.setMessage(crd.getMessage());
                ans.setCode(crd.getCode());
            }
        }

        return ans;
    }

    @Override
    @Transactional
    public CabinetResult set_text(int cabinetId, String text1, String text2, String text3) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&text1="+text1+"&text2="+text2+"&text3="+text3+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("text1",text1);
            map.put("text2",text2);
            map.put("text3",text3);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto cr=restTemplate.postForObject(url+"/set_text",request, CabinetResultDto.class);
            if(cr!=null){
                ans.setCode(cr.getCode());
                ans.setMessage(cr.getMessage());
            }

        }

        return ans;
    }

    @Override
    @Transactional
    public CabinetResult get_boxInfo(int cabinetId, int boxNo) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("boxNo",boxNo);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            ans = restTemplate.postForObject(url+"/get_boxInfo",request, CabinetResult.class);
        }

        return ans;
    }

    @Override
    public CabinetResult openOneBox(String s_id) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        BoxAndCabinet boxAndCabinet=boxAndCabinetMapper.findBoxByS_id(s_id);
        Cabinet cabinet=cabinetMapper.findCabinetById(boxAndCabinet.getCabinetId());
        String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxAndCabinet.getBoxNo()+"&cabinetId="+boxAndCabinet.getCabinetId()+"&timeStamp="+longStamp+cabinet.getSecretKey();
        String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cabinetId",cabinet.getCabinetId());
        map.put("boxNo",boxAndCabinet.getBoxNo());
        map.put("timeStamp",longStamp);
        map.put("sign",sign);
        map.put("appKey",cabinet.getAppKey());
        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
        CabinetResultDto crs = restTemplate.postForObject(url+"/openOneBox",request, CabinetResultDto.class);
        CabinetResult ans=new CabinetResult();
        if(ans!=null) {
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }

    @Override
    public CabinetResult openAllBox(int cabinetId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crs = restTemplate.postForObject(url+"/openAllBox",request, CabinetResultDto.class);
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }

    @Override
    public CabinetResult clearOneBox(int cabinetId, int boxNo, int optType, int isOpen) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&isOpen="+isOpen+"&optType="+optType+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("boxNo",boxNo);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("optType",optType);
            map.put("isOpen",isOpen);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crs = restTemplate.postForObject(url+"/clearOneBox",request, CabinetResultDto.class);
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }

    @Override
    public CabinetResult clearAllBox(int cabinetId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            String paramters="appKey="+cabinet.getAppKey()+"&cabinetId="+cabinetId+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crs = restTemplate.postForObject(url+"/clearAllBox",request, CabinetResultDto.class);
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }

    @Override
    public CabinetResult setOpenCode6(int cabinetId, int boxNo, int openCode6, int effectFlag, int isOpen) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            //String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&effectFlag="+effectFlag+"&openCode6="+openCode6+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&effectFlag="+effectFlag+"&isOpen="+isOpen+"&openCode6="+openCode6+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("boxNo",boxNo);
            map.put("openCode6",openCode6);
            map.put("effectFlag",effectFlag);
            map.put("isOpen",isOpen);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crs = restTemplate.postForObject(url+"/setOpenCode6",request, CabinetResultDto.class);
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }

    @Override
    public CabinetResult setOpenCode(int cabinetId, int boxNo, String openCode11, String pwd, int effectFlag, int isOpen) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String longStamp=String.valueOf(new Date().getTime());
        Cabinet cabinet=cabinetMapper.findCabinetById(cabinetId);
        CabinetResult ans=new CabinetResult();
        if(cabinet!=null){
            //String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&effectFlag="+effectFlag+"&openCode6="+openCode6+"&timeStamp="+longStamp+cabinet.getSecretKey();
            String paramters="appKey="+cabinet.getAppKey()+"&boxNo="+boxNo+"&cabinetId="+cabinetId+"&effectFlag="+effectFlag+"&isOpen="+isOpen+"&openCode11="+openCode11+"&pwd="+pwd
                    +"&timeStamp="+longStamp+cabinet.getSecretKey();
            String sign= DigestUtils.md5DigestAsHex(paramters.getBytes()).toLowerCase();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("cabinetId",cabinet.getCabinetId());
            map.put("boxNo",boxNo);
            map.put("openCode11",openCode11);
            map.put("pwd",pwd);
            map.put("effectFlag",effectFlag);
            map.put("isOpen",isOpen);
            map.put("timeStamp",longStamp);
            map.put("sign",sign);
            map.put("appKey",cabinet.getAppKey());
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
            CabinetResultDto crs = restTemplate.postForObject(url+"/setOpenCode",request, CabinetResultDto.class);
            ans.setMessage(crs.getMessage());
            ans.setCode(crs.getCode());
        }
        return ans;
    }


}
